import Api from '@/api/index'
import Store from '@/store'

/**
 * 判断两个json数组是否相等
 * @param {旧数组，新数组}
 * @return true数组不相等
 */
function hasChangedJsonList(oldList, newList) {
  let flag = false;
  if (oldList.length !== newList.length) {
    return true;
  }
  oldList.some((item, index) => {
    flag = JSON.stringify(item) !== JSON.stringify(newList[index]);
    return flag;
  })
  return flag;
}

function isMobile() {
  if (/(iPhone|iPad|iPod|iOS|Android)/i.test(navigator.userAgent)) { //移动端
    return true
  }
  return false
}

function setUser() {
  // 全局刷新用户信息
  Api.user.GetUserInfo().then(res => {
    Store.commit("setUserInfo", res.data.userInfo);
  })
}

function copyNumberId(copyText) {
  // 点击按钮复制订单号
  const input = document.createElement('input');
  input.setAttribute('readonly', 'readonly');
  input.setAttribute('value', copyText);
  document.body.appendChild(input);
  input.select();
  input.setSelectionRange(0, 9999);
  if (document.execCommand('copy')) {
    document.execCommand('copy');
    document.body.removeChild(input);
    return true;
  } else {
    return false;
  }
}

export default {
  hasChangedJsonList,
  isMobile,
  setUser,
  copyNumberId,
}